package com.intelligent.ispc.utils.executor;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * Created by Eligi.Ran on 2016/10/20.
 */
public class ExportThreadManager {
    private static ExportThreadManager exportThreadManager;
    private Map<String, Thread> spcExportThreads;
    private Map<String, Thread> grrExportThreads;
    private Map<String, Thread> yieldExportThreads;

    private ExportThreadManager() {
        spcExportThreads = new HashMap<>();
        grrExportThreads = new HashMap<>();
        yieldExportThreads = new HashMap<>();
    }

    public static ExportThreadManager getExportThreadManager() {
        if (exportThreadManager == null) {
            exportThreadManager = new ExportThreadManager();
        }
        return exportThreadManager;
    }

    public <T>FutureTask<T> initSpcExportThread(String threadName, Callable callable) {
        FutureTask<T> futureTask = new FutureTask<>(callable);
        Thread thread = new Thread(futureTask, threadName);
        spcExportThreads.put(threadName, thread);
        return futureTask;
    }

    public FutureTask<Boolean> initGrrExportThread(String threadName, Callable callable) {
        FutureTask<Boolean> futureTask = new FutureTask<>(callable);
        Thread thread = new Thread(futureTask, threadName);
        grrExportThreads.put(threadName, thread);
        return futureTask;
    }

    public FutureTask<Boolean> initYieldExportThread(String threadName, Callable callable) {
        FutureTask<Boolean> futureTask = new FutureTask<>(callable);
        Thread thread = new Thread(futureTask, threadName);
        yieldExportThreads.put(threadName, thread);
        return futureTask;
    }

    public void interruptSpcExport() {
        spcExportThreads.values().forEach(thread -> thread.interrupt());
        spcExportThreads.clear();
    }

    public void interruptGrrExport() {
        grrExportThreads.values().forEach(thread -> thread.interrupt());
        grrExportThreads.clear();
    }

    public void interruptYieldExport() {
        yieldExportThreads.values().forEach(thread -> thread.interrupt());
        yieldExportThreads.clear();
    }

    public void interruptAllExport() {
        interruptSpcExport();
        interruptGrrExport();
        interruptYieldExport();
    }

    public void startSpcExportThread() {
        spcExportThreads.values().forEach(thread -> thread.start());
    }

    public void startGrrExportThread() {
        grrExportThreads.values().forEach(thread -> thread.start());
    }

    public void startYieldExportThread() {
        yieldExportThreads.values().forEach(thread -> thread.start());
    }

    public Map<String, Thread> getSpcExportThreads() {
        return spcExportThreads;
    }


    public Map<String, Thread> getGrrExportThreads() {
        return grrExportThreads;
    }


    public Map<String, Thread> getYieldExportThreads() {
        return yieldExportThreads;
    }
}
